package com.lib.servlet.record;

import com.alibaba.fastjson.JSON;
import com.lib.common.Constant;
import com.lib.entity.JsonResult;
import com.lib.entity.Record;
import com.lib.service.RecordService;
import com.lib.service.ex.ServiceException;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "GetRecordServlet", value = "/admin/GetRecordServlet.do")
public class GetRecordListServlet extends HttpServlet {
    private RecordService recordService = new RecordService();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Record> recordList = null;
        JsonResult jsonResult = null;
        try {
            recordList = recordService.GetRecordList();
            jsonResult = JsonResult.getSuccessJR(recordList);
        } catch (ServiceException e) {
            e.printStackTrace();
            jsonResult = new JsonResult(Constant.JSON_STATE_SERVICE_ERROR,e.getMessage());
        }

        System.out.println("------jsonResult:"+ JSON.toJSONString(jsonResult));
        response.setCharacterEncoding("utf-8");
        response.getWriter().write(JSON.toJSONString(jsonResult));
        response.getWriter().flush();
        response.getWriter().close();
    }
}
